package com.vipheyue.campusrecyce.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.vipheyue.campusrecyce.R;
import com.vipheyue.campusrecyce.adapter.BaseRecyclerAdapter;
import com.vipheyue.campusrecyce.adapter.BaseRecyclerViewHolder;
import com.vipheyue.campusrecyce.bean.MyUser;
import com.vipheyue.campusrecyce.bean.ThingBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;

public class HavedReleaseActivity extends AppCompatActivity {
    @Bind(R.id.rlv_RecyclerView)
    RecyclerView recyclerView;
    private BaseRecyclerAdapter adapter;
    private List<ThingBean> mdata = new ArrayList<ThingBean>();
    private MyUser currentUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_haved_release);
        ButterKnife.bind(this);
        currentUser = BmobUser.getCurrentUser(this,MyUser.class);
        initview();
        setData();
    }

    private void initview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);//set view


        adapter = new BaseRecyclerAdapter<ThingBean>(this, mdata, R.layout.activity_fresh_item_collection) {
            @Override
            public void init(BaseRecyclerViewHolder holder, ThingBean bean, int position) {
                holder.setText(R.id.tv_data,  bean.getUpdatedAt());
                holder.setText(R.id.tv_name,bean.getName());
            }

        };
        adapter.setOnItemClickLinster(new BaseRecyclerAdapter.OnItemClickLinster() {
            @Override
            public void onItemClick(View view, int position) {
//                ThingBean bean =  mdata.get(position);
//                String jobStr = GsonUtils.toJson(bean);
//                Intent intent = new Intent(HavedReleaseActivity.this, TreasureDetailActivity.class);
//                intent.putExtra("jobStr", jobStr);
//                intent.putExtra("Longitude", 116.381936);
//                intent.putExtra("Latitude", 40.116547);
//                startActivity(intent);
            }
        });
        recyclerView.setAdapter(adapter);//Set adapter
    }

    private void setData() {
        BmobQuery<ThingBean> query = new BmobQuery<ThingBean>();
        query.order("-updatedAt");
        query.addWhereEqualTo("releaseUser", currentUser);

        query.findObjects(this, new FindListener<ThingBean>() {
            @Override
            public void onSuccess(List<ThingBean> object) {
                com.orhanobut.logger.Logger.d(object.size()+"");
                mdata.addAll(object);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(int code, String msg) {
                adapter.notifyDataSetChanged();
            }
        });
    }
}
